package com.infore.airsupervise.ui.activity;

import android.os.Bundle;
import android.view.View;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.biz.UserLocationBiz;
import com.infore.airsupervise.biz.impl.UserLocationBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.AmapUtil;
import com.infore.airsupervise.utils.JumpUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * @features:
 * @author: WuTao
 * @date: 2017/4/20 14:40
 */
public class MapPsonActivity
        extends BaseTitleBarActivity
        implements AMap.OnMapLoadedListener, AMap.OnInfoWindowClickListener ,AMap.OnMarkerClickListener {

    @BindView(R.id.map_view)
    MapView mapView;

    private AMap aMap;
    private UserLocationBiz locationBiz;
    private List<UserE> userEList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_pson);
        ButterKnife.bind(this);
        title.setText("人员");
        rightMenu.setVisibility(View.INVISIBLE);
        mapView.onCreate(savedInstanceState);
        initMapView();
    }

    /**
     * 初始化地图
     */
    private void initMapView(){
        if(aMap ==null) {
            aMap = mapView.getMap();
        }
        UiSettings uiSettings = aMap.getUiSettings();
        uiSettings.setZoomControlsEnabled(false);
        uiSettings.setScaleControlsEnabled(false);
        uiSettings.setMyLocationButtonEnabled(false);
        aMap.setOnMapLoadedListener(this);
        aMap.setOnMarkerClickListener(this);
        aMap.setOnInfoWindowClickListener(this);
    }

    /**
     * 地图加载成功
     */
    @Override
    public void onMapLoaded() {
        getPsonLocationData();
    }

    /**
     * 对marker标注点点击响应事件
    */
    @Override
    public boolean onMarkerClick(Marker marker) {
        if (marker.isInfoWindowShown()) {
            marker.hideInfoWindow();
        } else {
            aMap.animateCamera(CameraUpdateFactory.changeLatLng(marker.getPosition()));
            marker.showInfoWindow();
        }
        return true;
    }

    /**
     * 弹框点击事件
     * @param marker
     */
    @Override
    public void onInfoWindowClick(Marker marker) {
        UserE userE = (UserE) marker.getObject();
        JumpUtil.jumpFromTo(ctx, PersonDetailActivity.class,Constants.INTENT_OBJ_KEY, userE);
    }

    /**
     * 添加人员标记点
     * @param userE
     */
    private void addPsonMarker(UserE userE) {
        LatLng latLng = new LatLng(userE.getLat(),userE.getLnt());

        Marker marker = aMap.addMarker(new MarkerOptions().position(latLng)
                .title(userE.getRealName())
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker_person)));
        marker.setObject(userE);
        AmapUtil.startGrowAnimation(marker);
    }

    /**
     * 获取人员位置信息
     */

    private void getPsonLocationData(){
        if( null == locationBiz){
            locationBiz = new UserLocationBizImpl();
        }
        //loading dialog
        final SweetAlertDialog dialog = new SweetAlertDialog(ctx, SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText(MsgConst.HARD_LOADING)
                .setContentText(MsgConst.PLEASE_WAITING);
        dialog.setCancelable(false);
        dialog.show();
        if(null != aMap){
            aMap.clear();
        }
        locationBiz.getPsonLocationRequest(UserE.getUserInfo().getUserId(), new OnServerResponseListener<List<UserE>>() {
            @Override
            public void onSucceed(List<UserE> result, String desc) {

                //LatLngBounds.Builder builder = new LatLngBounds.Builder(); //地图可见区域
                for (UserE userE : result) {
                    //添加站点经纬度对象到地图可见区域
                  //  builder.include(addPsonMarker(userE,R.color.air_level_1));
                    addPsonMarker(userE);
                }
                //设置地图显示
               // aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(),32));
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(22.885674, 113.177242),12f));
                dialog.dismiss();
            }

            @Override
            public void onFailed(int errorCode, String title, String desc) {
                //dialog 错误 异常提示
                dialog.setTitleText(title)
                        .setContentText(desc)
                        .setConfirmText(MsgConst.I_SEE)
                        .changeAlertType(SweetAlertDialog.WARNING_TYPE);
            }
        });
    }

    /**
     * 刷新
     */
    @OnClick(R.id.btn_refresh)
    void refreshMarker() {
        getPsonLocationData();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

}
